package com.hrcx.mystery.controller;

import com.alibaba.fastjson.JSONObject;
import com.hrcx.mystery.common.CommonPage;
import com.hrcx.mystery.common.CommonResult;
import com.hrcx.mystery.common.PageParamRequest;
import com.hrcx.mystery.dto.HistorySearchDto;
import com.hrcx.mystery.dto.MysteryOpenDto;
import com.hrcx.mystery.dto.MysteryRecyclePrizeDto;
import com.hrcx.mystery.dto.MysterySearchDto;
import com.hrcx.mystery.vo.EbMysteryMerchandiseAssociatedVo;
import com.hrcx.mystery.vo.EbMysteryVo;
import com.hrcx.mystery.service.EbMysteryService;
import com.hrcx.mystery.vo.MysteryHistoryVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h1>盲盒前端请求控制器</h1>
 *
 * @author LinXiPeng
 * @className MysteryController
 * @date 2021/12/7 16:16
 * Version 1.0
 */
@RestController
@RequestMapping("api/mystery")
@Api(tags = {"盲盒模块（前台）"})
@Slf4j
public class MysteryController {
    /**
     * 注入盲盒服务
     */
    @Resource
    private EbMysteryService ebMysteryService;

    @ApiOperation("分页查询盒子列表(用户收藏的盲盒列表通用查询)")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<EbMysteryVo>> getList(@Validated MysterySearchDto searchRequest,
                                                         @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(ebMysteryService.getMysteryList(searchRequest, pageParamRequest)));
    }

    @ApiOperation("查询盲盒内的奖品")
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    @ApiImplicitParam(name = "bid", value = "盲盒的编号", dataType = "integer")
    public CommonResult<List<EbMysteryMerchandiseAssociatedVo>> getProdList(@RequestParam(value = "bid") Integer bid) {
        return CommonResult.success(ebMysteryService.getMysteryProdList(bid));
    }

    @ApiOperation("开启盲盒")
    @RequestMapping(value = "/openMystery", method = RequestMethod.POST)
    public CommonResult<List<EbMysteryMerchandiseAssociatedVo>> openMystery(@Validated MysteryOpenDto openRequest) {
        return CommonResult.success(ebMysteryService.openMystery(openRequest));
    }

    @ApiOperation("收藏盲盒/取消收藏")
    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    public CommonResult<JSONObject> collectMystery(@RequestParam("bid") Integer bid) {
        if (bid == null) {
            return CommonResult.failed("盲盒编号不能为空");
        }
        return ebMysteryService.collect(bid) ? CommonResult.success("操作成功") : CommonResult.failed("操作失败");
    }

    @ApiOperation("回收奖品")
    @RequestMapping(value = "/recyclePrize", method = RequestMethod.POST)
    public CommonResult<JSONObject> recyclePrize(@RequestBody @Validated MysteryRecyclePrizeDto recyclePrizeRequest) {
        return ebMysteryService.recyclePrize(recyclePrizeRequest) ? CommonResult.success("回收成功") : CommonResult.failed("回收失败");
    }

    @ApiOperation("查看我的开盒历史")
    @RequestMapping(value = "/getMyHistory",method = RequestMethod.GET)
    public CommonResult<CommonPage<MysteryHistoryVo>> getMysteryHistory(@Validated HistorySearchDto searchDto, @Validated PageParamRequest pageParamRequest){
        return CommonResult.success(CommonPage.restPage(ebMysteryService.getMysteryHistory(searchDto, pageParamRequest)));
    }
}